<?php
//=======================================
// !! GYSN RELEASE - DO NOT REMOVE  !!
// !! DeZend by: syairx             !!
// !! Debug by: syairx & str/GYSN   !!
// !! Nullify by: str/GYSN & syairx !!
// !! GYSN RELEASE - DO NOT REMOVE  !!
//=======================================

define( "SWIFTDEBUG", true );
define( "USESHUTDOWNFUNC", true );
@ini_set( 'session.use_trans_sid', false );
@ini_set( 'session.bug_compat_42', true );

if ( defined( "SWIFTCUSTOMPATH" ) )
{
    chdir( SWIFTCUSTOMPATH );
}
else
{
    chdir( dirname( __FILE__ ) );
}

error_reporting( E_ALL & ~E_NOTICE );

if ( PHP_VERSION < "4.1.0" )
{
    $_GET = &$HTTP_GET_VARS;
    $_POST = &$HTTP_POST_VARS;
    $_COOKIE = &$HTTP_COOKIE_VARS;
    $_SERVER = &$HTTP_SERVER_VARS;
    $_ENV = &$HTTP_ENV_VARS;
    $_FILES = &$HTTP_POST_FILES;
    $_REQUEST = array_merge( $_GET, $_POST, $_COOKIE );
}
if ( !defined( "ISMODERNBILL" ) && !defined( "ISMODERNBILLV5" ) )
{
    $_reserved = array( "_reserved", "GLOBALS", "_GET", "_POST", "_COOKIE", "_SERVER", "_ENV", "_REQUEST", "_FILES", "argv" );
    if ( is_array( $GLOBALS ) )
    {
        reset( $GLOBALS );
        while ( list( $_key, $_val ) = each( $GLOBALS ) )
        {
            if ( !in_array( $_key, $_reserved ) && ( $_key != "_key" ) && ( $_key != "_val" ) )
            {
            unset( $GLOBALS[$_key] );
            }
        }
    }
    unset( $GLOBALS["_key"] ); unset( $GLOBALS["_val"] );
}

function processGPC( &$arrayvar )
{
    if ( is_array( $arrayvar ) == true )
    {
        foreach ( $arrayvar AS $key => $val )
        {
            if ( is_string( $val ) == true )
            {
                $arrayvar[$key] = stripslashes( $val );
            }
            else if ( is_array( $val ) )
            {
                $arrayvar[$key] = processGPC( $val );
            }
        }
    }
    return $arrayvar;
}

if ( get_magic_quotes_gpc( ) == true )
{
    $_GET = processGPC( $_GET ); $_POST = processGPC( $_POST ); $_COOKIE = processGPC( $_COOKIE );
    if ( is_array( $_FILES ) )
    {
        foreach ( $_FILES AS $key => $val )
        {
            $_FILES[$key]['tmp_name'] = str_replace( '\\', '\\\\', $val['tmp_name'] );
        }
    }
    $_FILES = processGPC( $_FILES ); $_REQUEST = array_merge( $_POST, $_GET, $_COOKIE );
}

//set_magic_quotes_runtime( 0 );

unset( $_SWIFT );

if ( defined( "ISMODERNBILLV5" ) )
{
    global $_SWIFT, $interface, $template, $departments, $staffauth, $module, $grid, $xml, $staff, $settings, $dbCore, $events, $session, $loginshare, $cookie, $errormessage, $infomessage, $permissions, $mimelist, $registry, $datastore, $_MODULES;
}

require_once( "./includes/functions.php" );

$_SWIFT["starttime"] = fetchmicrotime( );
$_SWIFT["cwd"] = getcwd( );
$_SWIFT["timezone"] = 0;

require_once( "./config/config.php" );

require_once( "./includes/SmartyLight/class.template.php" );

$dbCore = new dbCore( $_DB["hostname"], $_DB["username"], $_DB["password"], $_DB["name"], $_DB["type"], false, $_DB["port"] );
$registry = new Registry;
$datastore = new DataStore;
$cookie = new Cookie;

$template = new Template;

if ( SWIFT_AREA != SWIFT_CLIENT && SWIFT_AREA != SWIFT_VISITOR )
{
    $template->languagetabletype = "file";
    $_SWIFT["theme"] = "admin_".cleanVar( $_SWIFT["admintheme"] );
    $template->assign( "languagecode", $_SWIFT["adminlanguage"] );
    $template->defaultLanguage = $_SWIFT["adminlanguage"];
    $template->loadLanguageTable( );
}

$events = new Events;
$module = new Module;
$interface = new CPInterface;

if ( SWIFT_AREA == SWIFT_ADMIN || SWIFT_AREA == SWIFT_STAFF || SWIFT_AREA == SWIFT_WINAPP || SWIFT_AREA == SWIFT_SYNCWORKS || SWIFT_AREA == SWIFT_SYNCML || SWIFT_AREA == SWIFT_MONITOR || SWIFT_AREA == SWIFT_RSS || SWIFT_AREA == SWIFT_PDA || SWIFT_AREA == SWIFT_INSTAALERT || SWIFT_AREA == SWIFT_MOBILE || SWIFT_AREA == SWIFT_WAP || SWIFT_AREA == SWIFT_API )
{
    $staffauth = new StaffAuth;
    $datastore->cache( "gridcache" );
    $datastore->cache( "groupsettingcache" );
    $datastore->cache( "departmentcache" );
    $datastore->cache( "commentcache" );
}
else if ( SWIFT_AREA == SWIFT_CLIENT || SWIFT_AREA == SWIFT_VISITOR )
{
    $datastore->cache( "languagecache" );
    $datastore->cache( "usergroupsettingcache" );
    require_once( "./includes/functions_users.php" );
}
else if ( SWIFT_AREA == SWIFT_CLI )
{
    $datastore->cache( "prioritycache" );
    $datastore->cache( "statuscache" );
    $datastore->cache( "queuecache" );
    $datastore->cache( "breaklinecache" );
    $datastore->cache( "staffcache" );
    $datastore->cache( "ticketcountcache" );
    $datastore->cache( "parserbanscache" );
    $datastore->cache( "parsercatchallcache" );
    $datastore->cache( "departmentcache" );
    $datastore->cache( "attachmenttypescache" );
    $datastore->cache( "slaplancache" );
    $datastore->cache( "slaschedulecache" );
}

$datastore->cache( "staffassigncache" );
$datastore->cache( "groupassigncache" );
$datastore->cache( "staffcache" );
$datastore->cache( "tgroupassigncache" );

$datastore->cache( "settingscache" );

$datastore->cache( "tgroupcache" );
$datastore->cache( "languagecache" );

$datastore->loadData( );

if ( !is_array( $_SWIFT["settingscache"] ) )
{
    echo "<BR /><font face='verdana,arial,helvetica' size='2' color='red'><B>".SWIFTPRODUCT." is not installed, Please run setup/index.php to run ".SWIFTPRODUCT."</B></font>";
    exit;
}

$settings = new Settings;
$_SWIFT["timezone"] = iif( !empty( $_SWIFT["settings"]["dt_timezone"] ), $_SWIFT["settings"]["dt_timezone"], 0 );
$_SWIFT["daylightsavings"] = iif( $_SWIFT["settings"]["dt_daylightsavings"] == 1, true, false );
$_SWIFT["stopwords"] = array( "GAY", "ASS", "SEX", "POO", "NIG", "SUK", "SUC", "WTF", "KKK", "FAG", "FUK", "FUC", "FFS", "LOL", "VAG", "DIK", "DIC", "FCK", "JIG", "KIL", "JEW", "CUM", "PUS", "POS", "JPG", "PNG", "GIF", "PHP", "PSP", "ASP", "DVD", "SQL", "ABC", "CPP", "NBC", "TXT", "DOC", "CNN", "PDF", "GOD", "BBC", "GOO", "PPT", "MSN", "AOL", "ICQ", "SWF", "AIM", "MEX", "JAP", "AZN", "PIG", "COW", "NIP", "CNT", "KFC", "DIE", "TOE", "HOE", "USA", "JPN", "DOG", "SOL", "PIS", "TIT", "STD", "WAP", "DED", "COK" );

$module->registered = $settings->getSection( "registeredmodules" );
$module->registered["core"] = "1";

$session = new Session;

if ( SWIFT_AREA == SWIFT_CLIENT || SWIFT_AREA == SWIFT_VISITOR || SWIFT_AREA == SWIFT_RSS )
{
    unset( $_tgroupid );

    require_once( "./includes/LoginShare/loginshare.php" );
    $loginshare = new LoginShareCore( );

    $_SWIFT["theme"] = "client_".cleanVar( $_SWIFT["clienttheme"] );
    $_SWIFT["user"] = array( );
    $_SWIFT["user"]["loggedin"] = false;

    $cookie->parseCookie( "client" );

    if ( trim( $_REQUEST["group"] ) != "" )
    {
        foreach ( $_SWIFT["tgroupcache"] as $key => $val )
        {
            if ( $val["title"] == strtolower( $_REQUEST["group"] ) )
            {
                $template->assign( "tgroupname", $val["title"] );

                $_tgroupid = $val["tgroupid"];
                break;
            }
        }
    }

    $cookiegroup = $cookie->getKey( "client", "groupid" );
    $cookielanguageid = $cookie->getKey( "client", "languageid" );

    $_buildcookie = false;
        if ( !empty( $_tgroupid ) )
        {
            $cookie->addCookie( "client", "groupid", $_tgroupid );
            $_SWIFT["tgroup"] = $_SWIFT["tgroupcache"][$_tgroupid];
            $template->setGroup( $_tgroupid );
            $_buildcookie = true;
        }
        else if ( defined( "ISMODERNBILLV5" ) && !empty( $_SWIFT["settings"]["g_mbtgroup"] ) )
        {
            $_SWIFT["tgroup"] = $_SWIFT["tgroupcache"][$_SWIFT["settings"]["g_mbtgroup"]];
            $template->setGroup( $_SWIFT["tgroup"]["tgroupid"] );
        }
        else if ( $cookiegroup != "" && isset( $_SWIFT["tgroupcache"][$cookiegroup] ) )
        {
            $_SWIFT["tgroup"] = $_SWIFT["tgroupcache"][$cookiegroup];
            $template->setGroup( $_SWIFT["tgroup"]["tgroupid"] );
        }
        else
        {
            foreach ( $_SWIFT["tgroupcache"] as $key => $val )
            {
                if ( ( $val["isdefault"] == 1 ) )
                {
                $_SWIFT["tgroup"] = $_SWIFT["tgroupcache"][$val["tgroupid"]];
                $_tisdefault = $val["tgroupid"];
                }
            }
        $template->setGroup( $_tisdefault );
        }

    $template->assign( "tgroupname", $_SWIFT["tgroup"]["title"] );
    $template->assign( "tgroup", $_SWIFT["tgroup"] );
    if ( !empty( $_SWIFT["tgroup"]["companyname"] ) )
    {
        $_SWIFT["settings"]["general_companyname"] = $_SWIFT["tgroup"]["companyname"];
    }

        if ( !empty($_GET["languageid"]) && !empty( $_SWIFT["languagecache"][$_GET["languageid"]]["languageid"] ) )
        {
            $languagecode = $_SWIFT["languagecache"][$_GET["languageid"]]["languagecode"];
            $cookie->addCookie( "client", "languageid", $_GET["languageid"] );
            $_buildcookie = true;

        }
        else if ( !empty($_GET["languageid"]) && !empty( $_SWIFT["languagecache"][$cookielanguageid]["languageid"] ) )
        {
            $languagecode = $_SWIFT["languagecache"][$cookielanguageid]["languagecode"];

        }
        else
        {
            $languagecode = $_SWIFT["languagecache"][$_SWIFT["tgroup"]["languageid"]]["languagecode"];
       
        if ( empty( $languagecode ) )
        {
        $languagecode = "en-us";
        }
}

    if ( $_buildcookie )
    {
        $cookie->buildCookie( "client", true );
    }

    $template->defaultLanguage = $languagecode;
    $template->activeLanguage = $languagecode;
    $template->assign( "languagecode", $languagecode );
    $template->languagetabletype = "db";
    $_SWIFT["clientlanguage"] = $languagecode;

    if ( empty( $_REQUEST["_m"] ) )
    {
        $datastore->cacheLanguageSection( "index" );
    }

    $template->loadLanguageTable( );
    $datastore->loadData( true );

    $template->assign( "languagelist", $_SWIFT["languagecache"] );

    if ( defined( "ISMODERNBILL" ) )
    {
        $_SWIFT["tgroup"]["loginapi_moduleid"] = LOGINAPI_MODERNBILL;
    }
    else if ( defined( "ISMODERNBILLV5" ) )
    {
        $_SWIFT["tgroup"]["loginapi_moduleid"] = LOGINAPI_MODERNBILLV5;
    }

    $loginshare->loadModule( );

	if ($_SWIFT["tgroup"]["grouppassword"] != "")
	{
		if (!isset($_SERVER["PHP_AUTH_USER"]) || md5($_SERVER["PHP_AUTH_PW"]) != $_SWIFT["tgroup"]["grouppassword"])
		{
		   header('WWW-Authenticate: Basic realm="'. $_SWIFT["language"]["groupaccess"] .'"'); 
		   header('HTTP/1.0 401 Unauthorized'); 
		   echo $_SWIFT["language"]["groupaccessmsg"];
		   exit;
		}
	}

    if ( $module->isRegistered( MODULE_KNOWLEDGEBASE ) )
    {
        $template->assign( "kbisregistered", true );
    }
    if ( $module->isRegistered( MODULE_DOWNLOADS ) )
    {
        $template->assign( "dlisregistered", true );
    }
    if ( $module->isRegistered( MODULE_TROUBLESHOOTER ) )
    {
        $template->assign( "trisregistered", true );
    }
}

$_SWIFT["themepath"] = $settings->getKey( "settings", "general_producturl" )."themes/".$_SWIFT["theme"]."/";
$_SWIFT["swiftpath"] = $settings->getKey( "settings", "general_producturl" );
$_SWIFT["IP"] = $_SERVER["REMOTE_ADDR"];
$_SWIFT["localepath"] = $_SWIFT["swiftpath"]."locale/".$_SWIFT["adminlanguage"]."/";

$template->register_function( "displaytemplate", "tpl_function_displayTemplate" );
$template->register_function( "urlencode", "tpl_function_urlencode" );
$template->register_function( "includeURL", "tpl_function_includeURL" );
$template->template_dir = "./themes/".$_SWIFT["theme"]."/";
$template->compile_dir = "./cache";
$template->caching = false;
$template->setPath( "./themes/".$_SWIFT["theme"]."/" );

$template->assign( "_SWIFT", $_SWIFT );
$template->assign( "settings", $_SWIFT["settings"] );
$template->assign( "themepath", $_SWIFT["themepath"] );
$template->assign( "swiftpath", $_SWIFT["swiftpath"] );
$template->assign( "localepath", $_SWIFT["localepath"] );
$template->assign( "version", $settings->getKey( "core", "version" ) );
$template->assign( "product", strtolower( SWIFTPRODUCT ) );
$template->assign( "productname", SWIFTPRODUCT );
$template->assign( "poweredbytitle", sprintf( $_SWIFT["language"]["poweredbytitle"], SWIFTPRODUCT, SWIFTVERSION ) );
$template->assign( "poweredby", sprintf( $_SWIFT["language"]["poweredby"], SWIFTPRODUCT, SWIFTVERSION ) );
$template->assign( "copyright", sprintf( $_SWIFT["language"]["copyright"], date( "Y" ) ) );
$template->assign( "date", edate( $_SWIFT["settings"]["dt_dateformat"] ) );
$template->assign( "xmlheader", "<?xml version=\"1.0\"?>" );
if ( SWIFT_AREA == SWIFT_STAFF )
{
    $template->assign( "jscachefile", $_SWIFT["settingscache"]["fileid"]["jscachefile"] );
}

if ( defined( "ISMODERNBILL" ) )
{
    $template->assign( "basepath", "user.php" );
    $_SWIFT["tgroup"]["loginapi_moduleid"] = LOGINAPI_MODERNBILL;

    if ( MBAREA == "news" )
    {
        $template->assign( "formextension", "<input type=\"hidden\" name=\"op\" value=\"menu\" /><input type=\"hidden\" name=\"tile\" value=\"mynews_tab\" />" );
        $template->assign( "baseurl", "user.php?op=menu&tile=mynews_tab&" );
    }
    else if ( MBAREA == "knowledgebase" )
    {
        $template->assign( "formextension", "<input type=\"hidden\" name=\"op\" value=\"menu\" /><input type=\"hidden\" name=\"tile\" value=\"faq\" />" );
        $template->assign( "baseurl", "user.php?op=menu&tile=faq&" );
    }
    else if ( MBAREA == "ticketsubmit" )
    {
        $template->assign( "formextension", "<input type=\"hidden\" name=\"op\" value=\"menu\" /><input type=\"hidden\" name=\"tile\" value=\"getsupport_tab\" />" );
        $template->assign( "baseurl", "user.php?op=menu&tile=getsupport_tab&" );
    }
    else if ( MBAREA == "ticketlist" )
    {
        $template->assign( "formextension", "<input type=\"hidden\" name=\"op\" value=\"menu\" /><input type=\"hidden\" name=\"tile\" value=\"mysupport\" />" );
        $template->assign( "baseurl", "user.php?op=menu&tile=mysupport&" );
    }
    else
    {
        $template->assign( "baseurl", "user.php?" );
    }
    $template->assign( "ismodernbill", true );
}
else if ( defined( "ISMODERNBILLV5" ) )
{
    $template->assign( "basepath", "helpdesk.php" );
    $_SWIFT["tgroup"]["loginapi_moduleid"] = LOGINAPI_MODERNBILLV5;
    if ( !( MBAREA == "ticketlist" ) )
    {
        if ( MBAREA == "knowledgebase" )
        {
            $template->assign( "formextension", "<input type=\"hidden\" name=\"action\" value=\"".getactionid( "ShowFAQs" )."\" /><input type=\"hidden\" name=\"sid\" value=\"".$CONF["user"]["sessionID"]."\" />" );
            $template->assign( "basepath", "faqs.php" );
            $template->assign( "baseurl", "faqs.php?action=".getactionid( "ShowFAQs" )."&sid=".$CONF["user"]["sessionID"]."&" );
        }
        else
        {
            $template->assign( "baseurl", "user.php?" );
        }
    }
    $template->assign( "ismodernbillv5", true );
}
else
{
    $template->assign( "basepath", $_SWIFT["swiftpath"]."index.php" );
    $template->assign( "baseurl", $_SWIFT["swiftpath"]."index.php?" );
    $template->assign( "ismodernbill", false );
}

if ( $_SWIFT["settings"]["t_scanirs"] == 1 && $module->isRegistered ( MODULE_KNOWLEDGEBASE ) && SWIFT_AREA == SWIFT_STAFF )
{
    $template->assign( "staffirs", true );
}
else
{
    $template->assign( "staffirs", false );
}

if ( $settings->getKey( "cron", "nextrun" ) <= time( ) )
{
    $template->assign( "runcron", true );
}

if ( version_compare(SWIFTVERSION, $_SWIFT["settingscache"]["core"]["version"]) == -1 )
{
    echo "<center><b><font color='red' face='verdana, arial, helvetica' size='2'>[WARNING]: Database Version Tag (".$_SWIFT["settingscache"]["core"]["version"].") and File (".SWIFTVERSION.") Mismatch!</font></b><BR /><font color='black' face='verdana, arial, helvetica' size='1'>Please update your files to the latest revision, This error happens when your database structure is different than the PHP files you are using.</font></center>\n<BR />";
}

$queuecount = $settings->getKey( "mail", "queuecount" );
if ( $queuecount > 0 )
{
    if ( !defined( "USESHUTDOWNFUNC" ) )
    {
        processMailQueue( );
    }
    else
    {
        registerShutdown( "mail" );
    }
}

if ( file_exists ( "./setup" ) && is_dir ( "./setup" ) && $_SERVER["REMOTE_ADDR"] != "192.168.0.47" && $_SERVER["REMOTE_ADDR"] != "192.168.0.19" && $_SERVER["REMOTE_ADDR"] != "" && $_SERVER["REMOTE_ADDR"] != "127.0.0.1" )
{
    echo "<center><b><font color='red' face='verdana, arial, helvetica' size='2'>[WARNING]: Setup Directory not Deleted</font></b><BR /><font color='black' face='verdana, arial, helvetica' size='1'>Please delete the setup directory before using the product, Keeping it on server can result in security breaches.</font></center>\n<BR />";
}
?>